package com.lucenly.card.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.lucenly.card.R;
import com.lucenly.card.base.BaseActivity;
import com.lucenly.card.event.StringMsgEvent;
import com.lucenly.card.utils.DataCleanManager;
import com.lucenly.card.view.dialog.ClearDialog;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import java.io.File;

import butterknife.BindView;

public class SettingActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.tv_cache)
    TextView tv_cache;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.tv_loginOut)
    TextView tv_loginOut;
    @BindView(R.id.ll_clear)
    LinearLayout ll_clear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
    }

    @Override
    protected void initView() {
        tv_title.setText("设置");
    }

    @Override
    protected void bindEvent() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ll_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                long folderSize = DataCleanManager.getFolderSize(new File(getCacheDir() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR));
                if(folderSize==0){
                    Toast.makeText(mContext, "没有缓存可清理", Toast.LENGTH_SHORT).show();
                    return;
                }
                DataCleanManager.deleteFolderFile(getCacheDir() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR,false);
                ClearDialog dialog = new ClearDialog(SettingActivity.this);
                dialog.show();
                    long folderSize2 = DataCleanManager.getFolderSize(new File(getCacheDir() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR));
                    String formatSize = DataCleanManager.getFormatSize(folderSize2);
                    tv_cache.setText(formatSize+"");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        tv_loginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferencesUtil.getInstance().putObject("user",null);
                SharedPreferencesUtil.getInstance().putString("Token","");
                startActivity(new Intent(SettingActivity.this,LoginActivity.class));
                BusManager.getBus().post(new StringMsgEvent("selectTab"));
                finish();
//                ActivityManager.getInstance().appExit(SettingActivity.this);
            }
        });
    }

    @Override
    protected void initData() {
        try {
            long folderSize = DataCleanManager.getFolderSize(new File(getCacheDir() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR));
            String formatSize = DataCleanManager.getFormatSize(folderSize);
            tv_cache.setText(formatSize+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        PackageInfo packageInfo = null;
        try {
            packageInfo = getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(getPackageName(), 0);
            tv_code.setText(packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void processClick(View view) {

    }
}
